package com.lufei.dh.view.Mult;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.lufei.dh.List.App;
import com.lufei.dh.R;
import com.lufei.dh.bottom.Adapter.Adapter_Drag;
import com.lufei.dh.view.Mult.Adapter.CategoryViewBinder;
import com.lufei.dh.view.Mult.Adapter.Lam.CategoryViewBinder2;
import com.lufei.dh.view.Mult.Adapter.Lam.SongViewBinder2;
import com.lufei.dh.view.Mult.Adapter.Lam.TwoViewBinder2;
import com.lufei.dh.view.Mult.Adapter.SongViewBinder;
import com.lufei.dh.view.Mult.Adapter.TwoViewBinder;
import com.lufei.dh.view.Mult.Bean.BeanData;
import com.lufei.dh.view.Mult.Bean.Category;
import com.lufei.dh.view.Mult.Bean.GsonUtils;
import com.lufei.dh.view.Mult.Bean.JuHeNewsEntity;
import com.lufei.dh.view.Mult.Bean.Song;
import com.lufei.dh.view.Mult.Bean.Two_Bean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ClassLinker;
import me.drakeet.multitype.ItemViewBinder;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：lufei
 * <p>
 * 创建日期：2018/6/29
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class CateActivity extends AppCompatActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private MultiTypeAdapter adapter;
    /*等同于ArrayList<Object> */
    private Items items;
    private List<Two_Bean> list = new ArrayList<>();
    private List<BeanData.ResultBean.DataBean> data = new ArrayList<>();
    private List<Integer> integers = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cate);
        ButterKnife.bind(this);
        setList();
        init();
    }

    private void init() {
        adapter = new MultiTypeAdapter();
        /* 注册类型和 View 的对应关系 */
//        adapter.register(Category.class, new CategoryViewBinder(this));
//        adapter.register(Song.class, new SongViewBinder(this));
//        adapter.register(Two_Bean.class, new TwoViewBinder(this));
        adapter.register(Song.class).to(new CategoryViewBinder2(this), new TwoViewBinder2(this), new SongViewBinder2(this)).

                withClassLinker(new ClassLinker<Song>() {
                    @NonNull
                    @Override
                    public Class<? extends ItemViewBinder<Song, ?>> index(@NonNull Song data) {
                        if (data.type == Song.Type_1) {
                            return CategoryViewBinder2.class;
                        } else if (data.type == Song.Type_2) {
                            return TwoViewBinder2.class;
                        } else if (data.type == Song.Type_3) {
                            return SongViewBinder2.class;
                        } else {
                            return CategoryViewBinder2.class;
                        }
                    }
                });

        recyclerView.setAdapter(adapter);
        items = new Items();
        int size = integers.size();
        for (int i = 0; i < size; i++) {
            BeanData.ResultBean.DataBean dataBean = data.get(i);
            if (integers.get(i) == 2) {
                items.add(new Song(dataBean.getUniquekey(), dataBean.getTitle(), dataBean.getDate(), dataBean.getCategory(), dataBean.getAuthor_name(), dataBean.getUrl(), dataBean.getThumbnail_pic_s(), dataBean.getThumbnail_pic_s02(), dataBean.getThumbnail_pic_s03(), i, 1));
//                items.add(new Two_Bean(dataBean.getUniquekey(),dataBean.getTitle(),dataBean.getDate(),dataBean.getCategory(),dataBean.getAuthor_name(),dataBean.getUrl(),dataBean.getThumbnail_pic_s(),dataBean.getThumbnail_pic_s02()));
            } else if (integers.get(i) == 1) {
//                items.add(new Category(dataBean.getUniquekey(),dataBean.getTitle(),dataBean.getDate(),dataBean.getCategory(),dataBean.getAuthor_name(),dataBean.getUrl(),dataBean.getThumbnail_pic_s()));
                items.add(new Song(dataBean.getUniquekey(), dataBean.getTitle(), dataBean.getDate(), dataBean.getCategory(), dataBean.getAuthor_name(), dataBean.getUrl(), dataBean.getThumbnail_pic_s(), dataBean.getThumbnail_pic_s02(), dataBean.getThumbnail_pic_s03(), i, 2));
            } else {
                items.add(new Song(dataBean.getUniquekey(), dataBean.getTitle(), dataBean.getDate(), dataBean.getCategory(), dataBean.getAuthor_name(), dataBean.getUrl(), dataBean.getThumbnail_pic_s(), dataBean.getThumbnail_pic_s02(), dataBean.getThumbnail_pic_s03(), i, 3));
            }
            Log.e("xxxz", i + "");
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
    }

    private void setAdapter() {

    }

    private void setList() {
//        String s1 = this.getString(R.string.json);
        String s1 = getJson("more.json", this);
        try {
            JSONObject jsonObject = new JSONObject(s1);
            JSONObject result = jsonObject.optJSONObject("result");
            JSONArray jsonArray = result.optJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject data = jsonArray.getJSONObject(i);
                int x = KJ(data);
                integers.add(x);
                this.data.add(new BeanData.ResultBean.DataBean(
                        data.optString("uniquekey"),
                        data.optString("title"),
                        data.optString("date"),
                        data.optString("category"),
                        data.optString("author_name"),
                        data.optString("url"),
                        data.optString("thumbnail_pic_s"),
                        data.optString("thumbnail_pic_s02"),
                        data.optString("thumbnail_pic_s03")
                ));
            }
            Log.e("xxxz", integers.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("xxxz", "11zzzz" + "\n" + e);
        }
//        for (int i = 0; i < beanData.getResult().getData().size(); i++) {
//       BeanData.ResultBean.DataBean bean= beanData.getResult().getData().get(i);
//            data.add(new BeanData.ResultBean.DataBean(bean.getUniquekey(),bean.getTitle(),bean.getDate(),bean.getCategory(),bean.getAuthor_name(),bean.getUrl(),bean.getThumbnail_pic_s(),bean.getThumbnail_pic_s02(),bean.getThumbnail_pic_s03()));
//        }
    }

    public static int KJ(JSONObject data) {
        try {
            String s = data.optString("thumbnail_pic_s");
            String s2 = data.optString("thumbnail_pic_s02");
            String s3 = data.optString("thumbnail_pic_s03");
            if (TextUtils.isEmpty(s2) && TextUtils.isEmpty(s3)) {
                return 1;
            } else if (TextUtils.isEmpty(s3)) {
                return 2;
            } else {
                return 3;
            }

        } catch (Exception e) {
            try {
                String s = data.optString("thumbnail_pic_s");
                String s2 = data.optString("thumbnail_pic_s02");
                return 2;
            } catch (Exception x) {

                try {
                    String s = data.optString("thumbnail_pic_s");
                    return 1;
                } catch (Exception v) {
                    return 0;
                }
            }
        }

    }

    public static String getJson(String fileName, Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
